﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace AllanSpartacus.Core.Serializations
{
    public static class JsonSerializer
    {
        public static T Deserialize<T>(string json)
        {
            var bytes = Encoding.Unicode.GetBytes(json);
            using (MemoryStream streamToRead = new MemoryStream(bytes))
            {
                var serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(streamToRead);
            }
        }

        public static string Serialize(object source)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                var serializer = new DataContractJsonSerializer(source.GetType());
                serializer.WriteObject(stream, source);
                stream.Position = 0;
                using (StreamReader streamReader = new StreamReader(stream))
                { return streamReader.ReadToEnd(); }
            }
        }

    }
}
